/**
 *   DataSlim - Lazy Loading Framework
 *   Copyright (C) 2007 Rodolfo Dias
 *   
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License along
 *   with this program; if not, write to the Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 
 */

package net.dataslim.invoke;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import net.dataslim.annotations.DataSlimReturn;

public class DataSlimResult {

	public Object proxied(Object result) throws Exception {
		if (result instanceof Collection){
			result = proxiedCollection( (Collection)result );
		}else if (result instanceof Map){
			//nao implementado ainda
		}else{
			result = proxiedObject(result);
		}
		return result;
	}
	
	public Object proxied(Object impl, Object result, String nameMethod, Class[] parameterTypes){
		if (result !=  null){
			Class[] interfaces = impl.getClass().getInterfaces();
			for(Class interfaceClass : interfaces){
				try{
					Method m = interfaceClass.getMethod(nameMethod, parameterTypes);
					if (m.isAnnotationPresent(DataSlimReturn.class)){
						return proxied(result);
					}
				}catch(Exception e){
					//e.printStackTrace(); Nao precisa fazer nada
					//apenas logar
				}
			}
		}
		return result;
	}
	
	protected Object proxiedObject(Object result){
		Object proxiedResult = result;
		try{
			Class resultClass = result.getClass();
			String packageName = resultClass.getPackage().getName()+".dataslim.";
			String nameClassProxied = packageName +  resultClass.getSimpleName()+"DataSlim";
			Class c = Class.forName(nameClassProxied, false, Thread.currentThread().getContextClassLoader());
		    proxiedResult = c.getConstructor(new Class[]{resultClass}).newInstance(result);
		}catch(Exception e){
			System.out.println("Classe dataslim nao encontrado para o objeto :" + result.getClass().getSimpleName() + " retornando objeto original.");
		}
		return proxiedResult;
	}
	
	protected Collection proxiedCollection(Collection resultCollection) throws Exception{
	    Collection resultCollectionProxied = resultCollection;
		if (!resultCollection.isEmpty()){
			resultCollectionProxied = new ArrayList();
			for(Iterator it = resultCollection.iterator(); it.hasNext();){
				Object o = it.next();
				resultCollectionProxied.add( proxiedObject(o) );
			}
			resultCollection = null;
		}
		return resultCollectionProxied;
	}
	
	protected void proxiedMap(Map resultMap){
		//TODO: Nao implementar por enquanto
	}
}
